< previous page page_116 next page >

Page 116
This code demonstrates a couple of useful techniques. First, just before the routine displays a new word, it checks to see if that word will fit on the current line of text. If the value of CurrentX plus the width of the new word in the current font is greater than the right margin max_x, the routine starts a new line of text.
e10a6a0a59f035e870d90cca05b85fce.gif
If CurrentX + TextWidth(new_word) > max_x Then
    CurrentX = min_x
    y = y + 1.25 * max_size
End If
RandomStyles also uses a technique for making the baselines of the words line up correctly. The CurrentX and CurrentY properties of a form determine where the upper-left corner of printed text appears. If you print words with different font sizes one after another, their tops line up at the Y coordinate value given by CurrentY. Text generally looks better if the words' baselines rather than their tops line up.
To make the words line up properly, RandomStyles keeps track of the Y value where each word's baseline should fall. It then computes a new value for CurrentY based on each word's font size with the following code:
e10a6a0a59f035e870d90cca05b85fce.gif
CurrentY = y + max_size - Font.Size
Example program Styles, shown in Figure 4.2, uses subroutine RandomStyles to draw text directly on a form using randomly selected colors and font styles. The form's AutoRedraw property is set to true, so the display is kept uptodate automatically. Whenever its form is resized, the program redraws the text using new random colors, sizes, and bold and italic attributes.
Notice that some of the words in Figure 4.2 look better than others. The word ''styles" at the end of the third line, for example, appears somewhat jagged. This can happen when Visual Basic must scale a font to fit the point size requested by the program. Some fonts are not designed to scale, and they produce good results only at certain fixed sizes. Stretching the font to another size produces a jagged result.
0116-01.jpg
Figure 4.2
Program Styles displays text with randomly selected fonts.

 
< previous page page_116 next page >